package chap08;

public class Thread_Demo {
    public static void main(String[] args) {
//        MyRunnable();
//        MyRunnable_A();
//        Thread_process();
//        Thread_Daemon();//设置守护后台
//        Thread_Multi();
//        Thread_Multi_2();
        Thread_last();
    }

    public static void Thead_demo() {
        TestThread t = new TestThread();
        t.start();//这个会同时进行
        for (int i = 0; i < 4; i++) {
            System.out.println(Thread.currentThread().getName() + "线程在运行");
        }
    }

    public static void MyRunnable() {
        MyRunnable mr = new MyRunnable();//这个是进行玩一个再一个
        Thread t = new Thread(mr);//到最后还是需要使用 mr容器去装载他
        t.start();
        for (int i = 0; i < 4; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

    public static void MyRunnable_A() {
        MyRunnable_lift mr = new MyRunnable_lift();
        Thread t = new Thread(mr);//到最后还是需要使用 mr容器去装载他
        t.start();
        for (int i = 0; i <= 4; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

    public static void Thread_process() {
        MyThread mt = new MyThread();
        mt.start();
        try {
            Thread.sleep(1500);
        } catch (InterruptedException e) //Interrupted 短剑
        {
            e.printStackTrace();
        }
        mt.setFlag(false);
    }

    public static void Thread_Daemon() {
        Thread mt = new Thread(new Thread_setdaemon());
        mt.setDaemon(true);
        mt.start();
        try {
            Thread.sleep(150);
        } catch (InterruptedException e) //Interrupted 短剑
        {
            e.printStackTrace();
        }
        System.out.println("end main");
    }

    public static void Thread_Multi() {
        Thread_same t = new Thread_same();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }

    public static void Thread_Multi_2() {
        Thread_same_B t = new Thread_same_B();
        new Thread(t).start();
        new Thread(t).start();
        new Thread(t).start();
    }

    public static void Thread_last () {
        last_demo water=new last_demo();
        ThreadA threadA=new ThreadA(water);
        ThreadB threadB = new ThreadB(water);
        threadB.start();
        threadA.start();
    }

}
class last_demo{
    boolean water=false;
    public boolean isEmpty(){
        return water? false:true;//如果没有水，就返回错
    }
    public void setWater(boolean haveWater){
        this.water=haveWater;
    }
}
class  ThreadA extends Thread{
    last_demo water;
    public ThreadA(last_demo water){
        this.water=water;
    }
    public void run(){
        System.out.println("开始进水");
        for(int i=0;i<=5;i++)
        {
            try{
                Thread.sleep(100);
                System.out.println(i+"分钟");
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
        water.setWater(true);
        System.out.println("进水完毕,水塘水满");
        synchronized (water){
            water.notify();//释放线程
        }
    }
}
class ThreadB extends  Thread{
    last_demo water;
    public ThreadB(last_demo water){
        this.water=water;
    }
    public void run (){
        System.out.println("启动排水");
        if(water.isEmpty()){
            synchronized (water){
                    try{
                        System.out.println("水塘无水，排水等待中");
                        water.wait();//线程等待
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
            }
        }
        System.out.println("开始排水");
        for(int i=5;i>=1;i--)
        {
            try{
                Thread.sleep(1000);
                System.out.println(i + "分钟");
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
        water.setWater(false);
        System.out.println("排水完毕");
    }
}


class Thread_same_B implements Runnable {
    private int tickets = 5;

    public void run() {
        while (true) {
//            synchronized (this) {
//                if (tickets > 0) {
//                    try {
//                        Thread.sleep(100);
//                        System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
//                    } catch (Exception E) {
//                        E.printStackTrace();
//                    }
//                }
//            }
            sale();
        }
    }

    public synchronized void sale() {
        if (tickets > 0) {
            try {
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName() + "出售票" + tickets--);
            } catch (Exception E) {
                E.printStackTrace();
            }
        }
    }
}

class Thread_same implements Runnable {
    private int ticket = 5;//这个是共用资源

    public void run() {
        while (true) {
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + "出售票" + ticket--);
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
        }
    }
}

class Thread_setdaemon implements Runnable {
    public void run() {
        while (true) {
            System.out.println(Thread.currentThread().getName() + "is running");
            try {
                Thread.sleep(150);//死循环 设置守护进程？
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyThread extends Thread {
    private boolean flag = true;

    public boolean isFlag() {
        return this.flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }

    public void run() {
        while (isFlag()) {
            try {
                Thread.sleep(500);
                System.out.println("运行一次业务");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (isFlag() == false) {
                return;//break
            }
        }
    }
}

class MyRunnable_lift implements Runnable {
    public void run() {
        for (int i = 0; i <= 4; i++) {
            try {
                Thread.sleep(500);//Thread 是一个静态类吗 还是创建的时候要对应着接口继承的属性？
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }
}

class MyRunnable implements Runnable {
    public void run() {//Runnable 中
        for (int i = 0; i <= 4; i++) {
            System.out.println(Thread.currentThread().getName() + " " + i);
        }
    }

}

class TestThread extends Thread {
    public void run() {
        for (int i = 0; i < 4; i++) {
            System.out.println(Thread.currentThread().getName() + "线程在运行");
        }
    }
}

